package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fz.entity.Marketing;
import com.fz.entity.MarketingGoods;
import com.fz.mapper.MarketingMapper;
import com.fz.service.MarketingGoodsService;
import com.fz.service.MarketingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 存储商城营销活动 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class MarketingServiceImpl extends ServiceImpl<MarketingMapper, Marketing> implements MarketingService {
    @Resource
    MarketingGoodsService marketingGoodsService;

    @Override
    @Transactional
    public Boolean addGoods(Long marketingId, Long[] goodsId) {
        System.out.println("goodsId"+ Arrays.toString(goodsId));
        // 查询出当前模块所关联的商品
        QueryWrapper<MarketingGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("marketing_id", marketingId);
        List<MarketingGoods> list = marketingGoodsService.list(wrapper);

        System.out.println(list);
        if(!list.isEmpty()) {
            ArrayList<Long> updateIds = new ArrayList<>();
            // 把传入的商品id存到hashSet
            HashSet<Long> hashSet = new HashSet<>(Arrays.asList(goodsId));
            ArrayList<Long> updateGoodsId = new ArrayList<>();
            // 查看当前的商品是否已经关联
            for (MarketingGoods marketingGoods : list) {
                // 如果当前对象的商品没被关联 记录数据库中的id
                if(!hashSet.contains(marketingGoods.getGoodsId())) {
                    updateIds.add(marketingGoods.getId());
                }else{
                    hashSet.remove(marketingGoods.getGoodsId());
                }
            }
            System.out.println(updateIds);
            System.out.println(hashSet);
            // 获取要修改的marketingGoods的id之后
            int i = 0;
            for (Long goodId : hashSet) {
                UpdateWrapper<MarketingGoods> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("goods_id", goodId).eq("id", updateIds.get(i++));
                marketingGoodsService.update(updateWrapper);
            }
        }else {
            for (Long goodId : goodsId) {
                if (!marketingGoodsService.save(new MarketingGoods(marketingId, goodId))) {
                    throw new RuntimeException("添加失败");
                }
            }
        }
        return true;
    }

    @Override
    public Marketing getById(Long id) {
        Marketing marketing = super.getById(id);
        QueryWrapper<MarketingGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("marketing_id", marketing.getId());
        List<MarketingGoods> list = marketingGoodsService.list(wrapper);
        List<Long> goodsId = new ArrayList<>();
        for (MarketingGoods marketingGoods : list) {
            goodsId.add(marketingGoods.getGoodsId());
        }
        marketing.setGoodsId(goodsId);
        return marketing;
    }

    @Override
    public List<Marketing> searchByName(String searchValue) {
        QueryWrapper<Marketing> wrapper = new QueryWrapper<>();
        wrapper.like("name", searchValue);
        return this.list(wrapper);
    }
}
